package cn.com.lyj6851.modules.shop.controller;

import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.exception.BadRequestException;
import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.aop.ForbidSubmit;
import cn.com.lyj6851.modules.shop.domain.YdySystemStore;
import cn.com.lyj6851.modules.shop.service.YdySystemStoreService;
import cn.com.lyj6851.modules.shop.service.dto.YdySystemStoreDto;
import cn.com.lyj6851.modules.shop.service.dto.YdySystemStoreQueryCriteria;
import cn.com.lyj6851.utils.location.GetTencentLocationVO;
import cn.com.lyj6851.utils.location.LocationUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

/**
*
 * @author Administrator
 */
@Api(tags = "门店管理")
@RestController
@RequestMapping("/api/ydySystemStore")
public class SystemStoreController {

    private final YdySystemStoreService yxSystemStoreService;
    private final IGenerator generator;
    public SystemStoreController(YdySystemStoreService yxSystemStoreService, IGenerator generator) {
        this.yxSystemStoreService = yxSystemStoreService;
        this.generator = generator;
    }


    @AopLog("所有门店")
    @ApiOperation("所有门店")
    @GetMapping(value = "/all")
    @PreAuthorize("@per.check('ydySystemStore:list')")
    public ResponseEntity<Object>  getAll(YdySystemStoreQueryCriteria criteria) {
        return new ResponseEntity<>(yxSystemStoreService.queryAll(criteria),HttpStatus.OK);
    }

    @AopLog("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@per.check('ydySystemStore:list')")
    public void download(HttpServletResponse response, YdySystemStoreQueryCriteria criteria) throws IOException {
        yxSystemStoreService.download(generator.convert(yxSystemStoreService.queryAll(criteria), YdySystemStoreDto.class), response);
    }

    @GetMapping
    @AopLog("查询门店")
    @ApiOperation("查询门店")
    @PreAuthorize("@per.check('ydySystemStore:list')")
    public ResponseEntity<Object> getYdySystemStores(YdySystemStoreQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxSystemStoreService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping(value = "/getL")
    @AopLog("获取经纬度")
    @ApiOperation("获取经纬度")
    @PreAuthorize("@per.check('ydySystemStore:getl')")
    public ResponseEntity<GetTencentLocationVO> create(@Validated @RequestBody String jsonStr) {
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String addr = jsonObject.getString("addr");
        GetTencentLocationVO locationVO = LocationUtils.getLocation(addr);
        if(locationVO.getStatus()!=0){
            throw new BadRequestException(locationVO.getMessage());
        }
        return new ResponseEntity<>(locationVO, HttpStatus.CREATED);
    }

    @ForbidSubmit
    @PostMapping
    @AopLog("新增门店")
    @ApiOperation("新增门店")
    @PreAuthorize("@per.check('ydySystemStore:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YdySystemStore resources){
        return new ResponseEntity<>(yxSystemStoreService.save(resources),HttpStatus.CREATED);
    }

    @ForbidSubmit
    @PutMapping
    @AopLog("修改门店")
    @ApiOperation("修改门店")
    @PreAuthorize("@per.check('ydySystemStore:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YdySystemStore resources){
        yxSystemStoreService.saveOrUpdate(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @AopLog("删除门店")
    @ApiOperation("删除门店")
    @PreAuthorize("@per.check('ydySystemStore:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        yxSystemStoreService.removeByIds(new ArrayList<>(Arrays.asList(ids)));
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
